Generally, we can categorize patterns in time series data into the following two categories:

Those patterns are either exists or not (e.g., a series may have a trend or not). We can express those components using the following notation for additive structure:

\[Y_t=T_t + C_t + S_t + I_t, \]

And for multiplicative structure:

\[Y_t=T_t \times C_t \times S_t \times I_t \]

For simplicity reasons, we will joined the cycle component into the trend, and rewrite the series components notation for additive structure:

\[Y_t=T_t + S_t + I_t, \] And for multiplicative structure:

\[Y_t=T_t \times S_t \times I_t \]

In this section, we will focus on decomposition methods of time series to its components - the trend, seasonal, and irregular.

Classical Decomposition

The classical decomposition (or by its full name - classical seasonal decomposition by moving average) is one of the most commom estimation method of the series components. R provides a built-in method for decompose time series with the decompose function from the stats package:

data("AirPassengers")

d <- decompose(AirPassengers)

str(d)
## List of 6
##  $ x       : Time-Series [1:144] from 1949 to 1961: 112 118 132 129 121 135 148 148 136 119 ...
##  $ seasonal: Time-Series [1:144] from 1949 to 1961: -24.75 -36.19 -2.24 -8.04 -4.51 ...
##  $ trend   : Time-Series [1:144] from 1949 to 1961: NA NA NA NA NA ...
##  $ random  : Time-Series [1:144] from 1949 to 1961: NA NA NA NA NA ...
##  $ figure  : num [1:12] -24.75 -36.19 -2.24 -8.04 -4.51 ...
##  $ type    : chr "additive"
##  - attr(*, "class")= chr "decomposed.ts"
plot(d)

d_m <- decompose(AirPassengers, type = "multiplicative")

plot(d_m)

library(TSstudio)

ts_decompose(AirPassengers, type = "both")
library(tsibble)
library(feasts)
library(fabletools)

ap_tsibble <- as_tsibble(AirPassengers)

decompose_md <- ap_tsibble %>% 
  model(classical_decomposition(value, type = "multiplicative"))
decompose_md %>% 
  components() %>% 
  head()
## # A dable:                 6 x 7 [1M]
## # Key:                     .model [1]
## # Classical Decomposition: value = trend * seasonal * random
##   .model                                                         index value trend seasonal random season_adjust
##   <chr>                                                          <mth> <dbl> <dbl>    <dbl>  <dbl>         <dbl>
## 1 "classical_decomposition(value, type = \"multiplicative\")" 1949 Jan   112    NA    0.910     NA          123.
## 2 "classical_decomposition(value, type = \"multiplicative\")" 1949 Feb   118    NA    0.884     NA          134.
## 3 "classical_decomposition(value, type = \"multiplicative\")" 1949 Mar   132    NA    1.01      NA          131.
## 4 "classical_decomposition(value, type = \"multiplicative\")" 1949 Apr   129    NA    0.976     NA          132.
## 5 "classical_decomposition(value, type = \"multiplicative\")" 1949 May   121    NA    0.981     NA          123.
## 6 "classical_decomposition(value, type = \"multiplicative\")" 1949 Jun   135    NA    1.11      NA          121.
decompose_md %>% 
  components() %>%
  autoplot()

d_tsibble <- decompose_md$`classical_decomposition(value, type = "multiplicative")`[[1]]$fit$decomposition

STL Decomposition

WIP